package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Supplier;

import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * description:
 * create by sxy 2021/6/8
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
        //
        PageHelper.startPage(pageNum, pageSize);

        Example o = new Example(Supplier.class);

        String address = supplierVO.getAddress();
        String contact = supplierVO.getContact();
        String name = supplierVO.getName();
        Example.Criteria criteria = o.createCriteria();
        //省市区的模糊查询

        if (address != null && !"".equals(address)) {
            criteria.andLike("address", "%" + address + "%");
        }
        //联系人查找
        if (contact != null && "".equals(contact)) {
            criteria.andEqualTo("contact", contact);
        }
        //地址查找
        if (name != null &&!"".equals(name)) {
            criteria.andEqualTo("name", name);
        }

        List<Supplier> supplierList = supplierMapper.selectByExample(o);

        List<SupplierVO> supplierVOS = new ArrayList<>();
        for (Supplier supplier : supplierList) {
            SupplierVO supplierVO1 = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO1);
            supplierVOS.add(supplierVO1);
        }
        PageInfo<Supplier> info = new PageInfo<>(supplierList);
        return new PageVO<>(info.getTotal(), supplierVOS);


    }

    //添加物资来源信息
    @Override
    public void add(SupplierVO supplierVO) throws SystemException {
        @NotBlank(message = "供应商名称不能为空") String name = supplierVO.getName();
        @NotBlank(message = "地址不能为空") String address = supplierVO.getAddress();
        Example o = new Example(Supplier.class);
        //判断物资来源是不是已经存在了
        List<Supplier> supplierList = supplierMapper.selectByExample(o);
        for (Supplier supplier : supplierList) {
            if (supplier.getAddress().equals(address)) {
                if (supplier.getName().equals(name)) {
                    throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该物资来源已经存在了");
                }
            }
        }

        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO, supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());

        supplierMapper.insert(supplier);


    }

    @Override
    public SupplierVO edit(Long id) {

        SupplierVO supplierVO = new SupplierVO();
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplier = supplierMapper.selectOne(supplier);
        BeanUtils.copyProperties(supplier, supplierVO);
        return supplierVO;
    }

    @Override
    public void update(Long id, SupplierVO supplierVO) throws SystemException {
        Supplier dbSupplier = supplierMapper.selectByPrimaryKey(id);
        @NotBlank(message = "地址不能为空") String address = supplierVO.getAddress();
        @NotBlank(message = "供应商名称不能为空") String name = supplierVO.getName();
        if (dbSupplier == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要修改的物资来源信息不存在");
        }

        Example example = new Example(Supplier.class);
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO, supplier);
        supplier.setId(id);
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKeySelective(supplier);


    }

    //删除物资来源信息
    @Override
    public void delete(Long id) throws SystemException {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if (supplier == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的物资来源信息不存在");
        }
        supplierMapper.deleteByPrimaryKey(id);
    }


    //入库添加时，加载来源的数据
    @Override
    public List<SupplierVO> findAll() {
        Example o = new Example(Supplier.class);
        List<Supplier> supplierList = supplierMapper.selectByExample(o);

        List<SupplierVO> supplierVOS = new ArrayList<>();
        for (Supplier supplier : supplierList) {
            SupplierVO supplierVO1 = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO1);
            supplierVOS.add(supplierVO1);
        }
        return supplierVOS;
    }
}
